class Service < ActiveRecord::Base
  attr_accessible :composite, :name, :binding_type, :description, :location, :location_abstract 

  def self.services_collect(composite_soa)
    comp_name =composite_soa.name
    composite_soa.services.collect do |serv|
      serv_name =serv.attribute('name').value
      service = Service.where(name: serv_name, composite: comp_name).first
      if service.nil?
	service = Service.new
	service.name = serv_name
	service.composite = comp_name
	fill_fields(service)
      end
      service.binding_type = composite_soa.binding_type(serv)
      service.location_abstract = composite_soa.wsdlLocation_abstract(serv)

      location = composite_soa.location(serv)
      if !location.nil? and (service.location.nil? or service.location.empty?)
	service.location = location
      end

      service.save if service.new_record? or service.changed?
      service
    end
  end
  
  def self.fill_fields(service)
    if service.description.nil? or service.description.length == 0
      serv = Service.find(:all, :conditions => "name = '#{service.name}' and description <> ''").first
      service.description = serv.description if !serv.nil?
    end
  end
end
